package com.pk.platform.business.flow.web;

import java.sql.Blob;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.pk.platform.business.core.web.GenericAction;
import com.pk.platform.business.flow.service.IFlowDefineService;
import com.pk.platform.business.flow.service.IFlowExecuteService;
import com.pk.platform.business.flow.vo.SubmitDocVO;
import com.pk.platform.domain.BaseBean;
import com.pk.platform.domain.flow.TableFlowRef;
import com.pk.platform.domain.flow.WorkFlow;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;

public class FlowExecuteAction extends GenericAction {

	private String tableName;
	
	protected String docId;
	
	private String flowId;
	
	private String nodeId;
	
	private String nodeName;
	
	private String status;
	
	private String tableFlowJson;
	
	private String actorName;
	
	private String actorStr;
	
	private TableFlowRef tfr = new TableFlowRef();
	
	private IFlowExecuteService flowExecuteService;
	
	private boolean condition;
	
	private String flowHistory;
	
	private String auditorInfo;
	
	private SubmitDocVO sdvo = new SubmitDocVO();
	
	private String returnBack;
	
	private int version;

	private int submitVersion;
	
	private List<Map<String, Object>> returnBackList = new ArrayList<Map<String,Object>>();
	
	private boolean auditorFlag;
	
	private String opinion;
	
	private String creatorId;

	private String currentUserId;
	
	private String flowType;
	
	private IFlowDefineService flowDefineService;
	
	private WorkFlow wf = new WorkFlow();
	
	private List<Map<String, Object>> statusHistoryList = new ArrayList<Map<String,Object>>();
	
	public String queryTableFlowRefById()
	{
		tfr = flowExecuteService.queryTableFlowRefById(tableName);
		if(tfr.getWorkFlow() != null)
		{
			Blob content = tfr.getWorkFlow().getContent();
			String contentStr = StringConverter.blobToString(content);
			tfr.getWorkFlow().setContentStr(contentStr);
		}
		tableFlowJson = JSONObject.toJSONString(tfr);
		return SUCCESS;
	}
	
	public String submitDoc()
	{
		sdvo.setTableName(tableName);
		sdvo.setDocId(docId);
		sdvo.setCurrentNodeId(nodeId);
		sdvo.setAuditorInfo(auditorInfo);
		sdvo.setReturnBack(returnBack);
		sdvo.setOpinion(opinion);
		sdvo.setVersion(version);
		sdvo.setSubmitVersion(submitVersion);
		if(StringConverter.isNotNull(auditorInfo))
		{
			sdvo.setAuditType(Constant.SUBMIT);
		}
		else if(StringConverter.isNotNull(returnBack))
		{
			sdvo.setAuditType(Constant.RETURN);
		}
		message = flowExecuteService.submitDoc(sdvo);
		return SUCCESS;
	}
	
	protected void setFlowInfo(BaseBean baseBean)
	{
		int version = baseBean.getVersion() == null ? 0 : baseBean.getVersion().intValue();
		int submitVersion = baseBean.getSubmitVersion() == null ? 0 : baseBean.getSubmitVersion().intValue();
		String creatorId = baseBean.getCreator() == null ? "" : baseBean.getCreator().getId();
		this.setDocId(baseBean.getId());
		this.setNodeId(baseBean.getNodeId());
		this.setNodeName(baseBean.getNodeName());
		this.setStatus(baseBean.getStatus());
		this.setVersion(version);
		this.setSubmitVersion(submitVersion);
		this.setCreatorId(creatorId);
		this.setCurrentUserId(super.getLoginUser().getId());
		String className = baseBean.getClass().getName();
		this.setTableName(className);
		flowHistory = flowExecuteService.queryFlowHistory(className, docId);
		if(StringConverter.isNotNull(baseBean.getId()))
		{
			returnBackList = flowExecuteService.queryFlowStatusHistory(className, docId);
		}
	}
	
	public String queryActorName()
	{
		actorName = flowExecuteService.queryActorName(actorStr);
		return SUCCESS;
	}
	
	public String startIsAuditor()
	{
		auditorFlag = flowExecuteService.startIsAuditor(auditorInfo);
		return SUCCESS;
	}
	
	public String taskIsAuditor()
	{
		auditorFlag = flowExecuteService.taskIsAuditor(tableName, docId);
		return SUCCESS;
	}
	
	public String assignToUser()
	{
		auditorInfo = flowExecuteService.queryAssignToUser(auditorInfo);
		return SUCCESS;
	}
	
	public String queryTheSameAuditor()
	{
		auditorInfo = flowExecuteService.queryTheSameAuditor(tableName, docId);
		return SUCCESS;
	}
	
	public String showFlow()
	{
		flowType = "update";
		tfr = flowExecuteService.queryTableFlowRefById(tableName);
		wf.setContentStr(StringConverter.blobToString(tfr.getWorkFlow().getContent()));
		statusHistoryList = flowExecuteService.queryDistinctFlowStatusSubmit(tableName, docId);
		return SUCCESS;
	}
	
	public String testCondition()
	{
		condition = true;
		return SUCCESS;
	}
	
	public String testActors()
	{
		actorStr = "2|role;4|role";
		return SUCCESS;
	}
	
	public String testAction()
	{
		System.out.println("test action");
		return SUCCESS;
	}
	
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getDocId() {
		return docId;
	}

	public void setDocId(String docId) {
		this.docId = docId;
	}

	public String getFlowId() {
		return flowId;
	}

	public void setFlowId(String flowId) {
		this.flowId = flowId;
	}

	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getTableFlowJson() {
		return tableFlowJson;
	}

	public void setTableFlowJson(String tableFlowJson) {
		this.tableFlowJson = tableFlowJson;
	}

	public TableFlowRef getTfr() {
		return tfr;
	}

	public void setTfr(TableFlowRef tfr) {
		this.tfr = tfr;
	}

	public IFlowExecuteService getFlowExecuteService() {
		return flowExecuteService;
	}

	public void setFlowExecuteService(IFlowExecuteService flowExecuteService) {
		this.flowExecuteService = flowExecuteService;
	}

	public String getActorName() {
		return actorName;
	}

	public void setActorName(String actorName) {
		this.actorName = actorName;
	}

	public String getActorStr() {
		return actorStr;
	}

	public void setActorStr(String actorStr) {
		this.actorStr = actorStr;
	}

	public boolean isCondition() {
		return condition;
	}

	public void setCondition(boolean condition) {
		this.condition = condition;
	}

	public String getFlowHistory() {
		return flowHistory;
	}

	public void setFlowHistory(String flowHistory) {
		this.flowHistory = flowHistory;
	}

	public String getAuditorInfo() {
		return auditorInfo;
	}

	public void setAuditorInfo(String auditorInfo) {
		this.auditorInfo = auditorInfo;
	}

	public SubmitDocVO getSdvo() {
		return sdvo;
	}

	public void setSdvo(SubmitDocVO sdvo) {
		this.sdvo = sdvo;
	}

	public String getReturnBack() {
		return returnBack;
	}

	public void setReturnBack(String returnBack) {
		this.returnBack = returnBack;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public int getSubmitVersion() {
		return submitVersion;
	}

	public void setSubmitVersion(int submitVersion) {
		this.submitVersion = submitVersion;
	}

	public List<Map<String, Object>> getReturnBackList() {
		return returnBackList;
	}

	public void setReturnBackList(List<Map<String, Object>> returnBackList) {
		this.returnBackList = returnBackList;
	}

	public boolean getAuditorFlag() {
		return auditorFlag;
	}

	public void setAuditorFlag(boolean auditorFlag) {
		this.auditorFlag = auditorFlag;
	}

	public String getOpinion() {
		return opinion;
	}

	public void setOpinion(String opinion) {
		this.opinion = opinion;
	}

	public String getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(String creatorId) {
		this.creatorId = creatorId;
	}

	public String getCurrentUserId() {
		return currentUserId;
	}

	public void setCurrentUserId(String currentUserId) {
		this.currentUserId = currentUserId;
	}

	public String getFlowType() {
		return flowType;
	}

	public void setFlowType(String flowType) {
		this.flowType = flowType;
	}

	public IFlowDefineService getFlowDefineService() {
		return flowDefineService;
	}

	public void setFlowDefineService(IFlowDefineService flowDefineService) {
		this.flowDefineService = flowDefineService;
	}

	public WorkFlow getWf() {
		return wf;
	}

	public void setWf(WorkFlow wf) {
		this.wf = wf;
	}

	public List<Map<String, Object>> getStatusHistoryList() {
		return statusHistoryList;
	}

	public void setStatusHistoryList(List<Map<String, Object>> statusHistoryList) {
		this.statusHistoryList = statusHistoryList;
	}

}
